//
//  FKLoginVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/13.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKLoginVC.h"
#import <UMShare/UMShare.h>
#import "FKBindMobileVC.h"
#import "FKCustomBtn.h"

@interface FKLoginVC ()

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet FKCustomBtn *mobileLoginBtn;

@property (nonatomic, strong) NSMutableDictionary *wechatInfo;

@end

@implementation FKLoginVC

- (NSMutableDictionary *)wechatInfo
{
    if (!_wechatInfo) {
        _wechatInfo = [NSMutableDictionary dictionary];
    }
    return _wechatInfo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登录";
    
    [self.loginBtn setBackgroundImage:[UIImage fk_imageWithColor:[UIColor fk_colorWithHex:0x068900]] forState:UIControlStateNormal];
    self.mobileLoginBtn.imagePosition = FKImagePositionRight;
    
    UIButton *leftItem = [UIButton fk_btnWithTarget:self action:@selector(close) img:[UIImage imageNamed:@"dissmiss"]];
    leftItem.bounds = CGRectMake(0, 0, 44, 44);
    leftItem.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftItem];
}

- (void)close
{
    if (self.navigationController) {
        [self.navigationController dismissViewControllerAnimated:true completion:nil];
    }
}


- (IBAction)loginBtnClicked:(UIButton *)sender
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            [SVProgressHUD showInfoWithStatus:@"获取授权信息失败"];
        } else {
            if ([result isKindOfClass:[UMSocialUserInfoResponse class]]) {
                UMSocialUserInfoResponse *resp = result;
                self.wechatInfo[@"headimgurl"] = resp.iconurl;
                self.wechatInfo[@"nickname"] = resp.name;
                if ([resp.gender isEqualToString:@"m"]) {
                    self.wechatInfo[@"sex"] = @"1";
                } else if ([resp.gender isEqualToString:@"w"]) {
                    self.wechatInfo[@"sex"] = @"2";
                } else {
                    self.wechatInfo[@"sex"] = @"0";
                }
                self.wechatInfo[@"openid"] = resp.openid;
                self.wechatInfo[@"unionid"] = resp.unionId;
//                [self checkOpenid:resp.openid];
                [self login:self.wechatInfo];
            } else {
                [SVProgressHUD showInfoWithStatus:@"获取授权信息失败"];
            }
        }
    }];
}

- (IBAction)mobileBtnClicked:(UIButton *)sender
{
    FKBindMobileVC *vc = [[FKBindMobileVC alloc] init];
    vc.type = @"1";
    fkPushNav(vc);
}


#pragma mark - 网络请求
- (void)login:(NSDictionary *)param
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Login&a=iosLogin" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSDictionary *userInfo = result.data[@"user_info"];
            // 保存登录信息，而不是用户信息
            [FKCacheTool saveLoginInfo:result.data];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"changeTabBar" object:nil];
            if ([fkFORMAT(@"%@", userInfo[@"bind_mobile"]) isEqualToString:@"1"]) { // 已经绑定手机号
                [FKLoginTool changeRootVC];
            } else {
                NSDictionary *areaInfo = result.data[@"area_info"];
                self.wechatInfo[@"area_id"] = areaInfo[@"id"];
                FKBindMobileVC *vc = [[FKBindMobileVC alloc] init];
                vc.type = @"2";
                fkPushNav(vc);
            }
        }
    } failure:nil];
}

///** 判断是否已经注册，从而判断是否需要绑定手机号 */
//- (void)checkOpenid:(NSString *)openId
//{
//    NSMutableDictionary *param = [NSMutableDictionary dictionary];
//    param[@"openid"] = openId;
//    param[@"type"] = @"2";
//    [FKHttpTool postWithSuffix:@"m=Home&c=Login&a=checkOpenid" param:param success:^(FKBaseResult *result) {
//        if (result.isSuccess) {
//            if ([result.data[@"is_reg"] isEqualToNumber:@(1)]) {
//                [SVProgressHUD showSuccessWithStatus:@"登录成功"];
//                // 保存登录信息
//                [FKCacheTool saveLoginInfo:result.data];
//                [FKLoginTool changeRootVC];
////                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeTabBar" object:nil];
////                [self.navigationController dismissViewControllerAnimated:true completion:nil];
//            } else { // 没有注册过，跳转到绑定手机号
//                NSDictionary *areaInfo = result.data[@"area_info"];
//                self.wechatInfo[@"area_id"] = areaInfo[@"id"];
//                FKBindMobileVC *vc = [[FKBindMobileVC alloc] init];
//                vc.type = @"2";
//                vc.param = self.wechatInfo;
//                fkPushNav(vc);
//            }
//        }
//    } failure:nil];
//}

@end
